"""
@author：yiwang
@file_name: __init__.py
@create date: 2023-09-01
@file_description：
"""
from apps.API.common.common_time import *
from flask_restful import reqparse
from apps.API.connector.db_common import query_info

class create_msyql_sql(object):
    def __init__(self,data_table):
        self.data_table = data_table
        
    def create_query_infos(self):
        table_cols = ""
        for _,values in self.data_table["match"].items():
            values = values.split("|")[0]
            table_cols = table_cols + values + ","
        table_cols = table_cols.rstrip(",")
        query_sql = '''
            SELECT
                {table_cols}
            FROM 
                {table_name}
        '''.format(
            table_cols = table_cols,
            table_name = self.data_table["name"]
        )
        base_infos = query_info(query_sql)
        infos = []
        keys = list(self.data_table["match"].keys())
        for base_info in base_infos:
            tmp = {}
            for index in range(0,len(keys)):
                tmp[keys[index]] = base_info[index]
            infos.append(tmp)
        return infos
    
    def create_reqparser(self):
        parser = reqparse.RequestParser()
        for key,values in self.data_table["match"].items():
            type_name = values.split("|")[1]
            parser.add_argument("\"{key}\"".format(key=key),type=type_name,required=True,help="{key}输入有误！".format(key=key))
        args = parser.parse_args()        
        return args

    def create_insert_sql(self,args):
        # ### STEP 1: create reqparser ###
        # args = self.create_reqparser()
        ### STEP 2: create insert sql ###
        table_cols = ""
        table_values = ""
        for key,values in self.data_table["match"].items():
                values = values.split('|')[0]
                table_cols = table_cols + values + ","
                if key == "md_time":
                    table_values = table_values + "\"" + get_current_time() + "\"" + ","
                else:
                    if type(args.get(key)) == int or type(args.get(key)) == float:
                        table_values = table_values + str(args.get(key)) + ","
                    elif type(args.get(key)) == str:
                        table_values = table_values + "\"" + args.get(key) + "\"" + ","
                    else:
                        table_values = table_values + "\"" + str(args.get(key)) + "\"" + ","
        table_cols = table_cols.rstrip(",")
        table_values = table_values.rstrip(",")
        ### SQL：add qc infos to database ###
        add_sql = '''
            INSERT INTO {table_name}(
                {table_cols}
            ) VALUES (
                {table_values}
            )
        '''.format(
            table_name = self.data_table["name"],
            table_cols = table_cols,
            table_values = table_values
        )
        return add_sql

    def create_query_sql(self,cols,table_name):
        query_sql = '''
            SELECT 
                {cols}
            FROM {table_name}
        '''.format(
            cols = cols,
            table_name = table_name
        )
        return query_sql
        
    def create_put_sql(self,args):
        ### STEP 2: create insert sql ###
        table_cols = ""
        table_values = ""
        for key,values in self.data_table["match"].items():
            print(key,values)
        ### SQL：add extraction qc infos to database ###

class common_tables(object):
    sample_db = {
        "name": "sms_sample",
        "match":{
            "sam_id":"sample_id|str",
            "sam_name":"sample_name|str",
            "lot_number":"lot_number|str",
            "size":"size|int",
            "unit":"unit|str",
            "sample_amount":"sam_amount",
            "op_user":"operate_user|str",
            "ch_user":"check_user|str"
        }
    }
    project_db = {
        "name": "project",
        "match":{
            "sam_id":"sample_id|str",
            "sam_name":"sample_name|str",
            "lot_number":"lot_number|str",
            "size":"size|int",
            "unit":"unit|str",
            "sample_amount":"sam_amount",
            "op_user":"operate_user|str",
            "ch_user":"check_user|str"
        },
        "cols": {
            "id":"id",
            "project_id":"project_id",
            "client_id":"client_id",
            "sample_id":"sample_id",
            "sample_name":"sample_name",
            "lot_number":"lot_number",
            "size":"size",
            "sample_amount":"sample_amount",
            "arrive_time":"arrive_time",
            "instock_time":"instock_time",
            "create_time":"create_time",
            "modify_time":"modify_time"
        }
    }
    service_db = {
        "name": "sms_service",
        "cols": """
            pass
        """
        
    }
    receipt_db = {
        "name": "sms_receipt",
        "cols": {}
    }
    users_db = {
        "name": "users"
    }
    role_db = {
        "name": "roles"
    }
    system_log_db = {
        "name": "sys_log"
    }
    disp_db = {
        "name": "sms_sample_disp"
    }
    sample_destory_db = {
        "name": "sample_destory"
    }
    